var QQMapWX = require('../../utils/api/qqmap-wx-jssdk.min');
var qqmapsdk;
const { Tab } = require('../../assets/libs/zanui/index');
const innerAudioContext = wx.createInnerAudioContext();//录音播放实例
let interval = "";
var app = getApp();
Page(Object.assign({}, Tab, {
	data: {
		isHidden: true,
		cdnUrl: '',
		population: 0, // 服务态度评分点数初始值
		distribution: 0,// 准时率评分点数初始值
		recordCountDown: '00:00:00',
		recordDuration: 0,//默认音频时长
		
		bannerList: [],
		archivesList: [],
		loading: false,
		nodata: false,
		nomore: false,
		tab: {
			list: [],
			selectedId: '0',
			scroll: true,
			height: 44
		},
		/* 自定义导航栏参数 */
		loading: false,
		color: '#FFFFFF',
		background: '#FFFFFF',
		show: true,
		animated: false,
		back: true,
		title: "订单详情",
		
		orderDetail: {},
		/* 评价信息 */
		evaluateContent: ''//评价文字内容
	},
	channel: 0,
	page: 1,
	callphone (e) {
		wx.makePhoneCall({
			phoneNumber: e.currentTarget.dataset.mobile
		})
	},
	onLoad: function (e) {
		qqmapsdk = new QQMapWX({
			key: 'JVJBZ-MWUWQ-4XO5M-G3HPS-Y4E6T-XUFX4'
		});
		var id = e.id;
		var that = this;
		that.setData({
			cdnUrl: app.cdnUrl
		});
		that.orderDetail(id);
	},
	/*地图导航*/
	onNavigate () {
		var that = this;
		qqmapsdk.search({
			keyword: that.data.orderDetail.address + that.data.orderDetail.address_detail,
			success: function (res) {
				console.log(res);
				var location = res.data[0]['location'];
				wx.openLocation({
					/** 纬度，范围为-90~90，负数表示南纬 */
					'latitude': location.lat,
					/** 经度，范围为-180~180，负数表示西经 */
					'longitude': location.lng,
					/** 位置名 */
					'name': that.data.orderDetail.address + that.data.orderDetail.address_detail,
					/** 地址的详细说明 */
					'address': that.data.orderDetail.address + that.data.orderDetail.address_detail
				})
			},
			fail: function (res) {
				console.log(res);
			},
		})
	},
	/* 获取订单详情 */
	orderDetail: function (id) {
		var that = this;
		app.request('/api/timetable.user/orderDetail', {
			id: id
		}, function (data, ret) {
			that.setData({
				orderDetail: data
			});
			innerAudioContext.src = data.recordfile;
		}, function (data, ret) {
			app.error(ret.msg);
			setTimeout(function () {
				wx.navigateBack({});
			}, 3000)
		});
	},
	/* 录音播放*/
	play: function () {
		var that = this;
		innerAudioContext.play();
		innerAudioContext.onPlay(function () {
			that.count_down(innerAudioContext.duration.toFixed(0));
		})
		innerAudioContext.onEnded(function () {
			console.log("播放结束");
		})
	},
	//点击 停止
	stop: function () {
		innerAudioContext.pause();
	},
	count_down: function (remaintime) {
		var that = this;
		var expireMs = remaintime;
		that.setData({
			recordCountDown: that.date_format(expireMs)
		});
		if ( expireMs==0 ) {
			clearTimeout(interval);
		} else {
			interval = setTimeout(function () {
				expireMs = expireMs - 1;
				that.count_down(expireMs);
			}, 1000)
		}
		
	},
	/* 格式化倒计时 */
	/* 格式化倒计时 */
	date_format: function (seconds) {
		if ( seconds<0 ) seconds = 0;
		var that = this;
		// var day = that.fill_zero_prefix(Math.floor(seconds / 86400)) //天
		var folur = that.fill_zero_prefix(Math.floor((seconds%86400)/3600))//小时
		var min = that.fill_zero_prefix(Math.floor((seconds%3600)/60));// 分钟位
		var sec = that.fill_zero_prefix(seconds%60); // equal to => var sec = second % 60;// 秒位
		return folur + ':' + min + ':' + sec;
	},
	fill_zero_prefix: function (num) {
		return num<10 ?"0" + num:num
	},
	
	onShareAppMessage: function () {
		return {
			title: '狮欧回收',
			desc: '',
			path: '/page/index/index'
		}
	},
	/* 弹窗 */
	showMd: function (e) {
		var that = this;
		// var id = e.currentTarget.dataset.id - 1;
		that.setData({
			isHidden: false//显示模态框，
		})
	},
	hiddenMd: function () {
		this.setData({
			isHidden: true//隐藏模态框
		})
	}
	
}))
